﻿using B.S.Traceability.Myproject.Domain;
using B.S.Traceability.Myproject.ErrorCode;
using B.S.Traceability.Myproject.infrastructure.Interface;
using B.S.Traceability.MyRead.Api.Appicant.Command.InformationCommand;
using B.S.Traceability.MyRead.Api.DTOs;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Traceability.MyRead.Api.Appicant.CommandHandler.InformationCommandHandler
{

    public class GetInformationCommandHandler : IRequestHandler<GetInformationCommand, APIPaing<GetInformationDTO>>
    {
        private readonly IBaseRepository<Information> informationRepos;
        private readonly IBaseRepository<ShopMondel> shopMondelRepos;

        public GetInformationCommandHandler(IBaseRepository<Information> InformationRepos, IBaseRepository<ShopMondel> ShopMondelRepos)
        {
            informationRepos = InformationRepos;
            shopMondelRepos = ShopMondelRepos;
        }

        public async Task<APIPaing<GetInformationDTO>> Handle(GetInformationCommand request, CancellationToken cancellationToken)
        {
            APIPaing<GetInformationDTO> result = new APIPaing<GetInformationDTO>();

            var infor = await informationRepos.GetAll().Result.Where(x => x.Isdel == 1).ToListAsync();

            var roles = await shopMondelRepos.GetAll().Result.Where(x => x.Status == 1).ToListAsync();



            var list = from a in infor
                       join b in roles on a.ShopName equals b.Id
                       select new GetInformationDTO
                       {
                           Id = a.Id,
                           ShopNameId = a.ShopName,
                           batch = a.batch,
                           ShelfLife = a.ShelfLife,
                           ProductionLicense = a.ProductionLicense,
                           Manufacturers = a.Manufacturers,
                           image = a.image,
                           completeness = a.completeness,
                           Isdel = a.Isdel,
                           ShopName = b.ShopName,
                           Status = b.Status,
                       };

            //////条件查询///////

            if (!string.IsNullOrEmpty(request.ShopName))
            {
                list = list.Where(x => x.ShopName.Contains(request.ShopName)).ToList();
            }
            if (request.batch != null)
            {
                list = list.Where(x => x.batch == request.batch).ToList();
            }
            if (request.ShelfLife != null)
            {
                list = list.Where(x => x.ShelfLife == request.ShelfLife).ToList();
            }
            if (request.ProductionLicense != null)
            {
                list = list.Where(x => x.ProductionLicense == request.ProductionLicense).ToList();
            }
            if (request.Manufacturers != null)
            {
                list = list.Where(x => x.Manufacturers == request.Manufacturers).ToList();
            }
            //总页数
            int toTalCount = list.Count();
            //分页数
            int toTalPage = (int)Math.Ceiling(toTalCount * 1.0 / request.TotalSize);
            //分页
            list = list.OrderByDescending(x => x.Id).Skip((request.TotalIndex - 1) * request.TotalSize).Take(request.TotalSize);
            if (list.Count() < 0)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
                result.TotalCount = toTalCount;
                result.TotalPage = toTalPage;
                result.Items = list.ToList();
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.TotalCount = toTalCount;
                result.TotalPage = toTalPage;
                result.Items = list.ToList();
            }

            return await Task.FromResult(result);
        }


    }
}
